import storageApi from '../../core/storage/storage';

function patrolBranchController($rootScope, $scope, patrolService) {
  'ngInject';

  /*eslint-disable */
  this.currentPage = 1;

  var date = new Date();

  // 处理日期
  this.dateToStr = (date, format) => {
    var date_arr = {
      "M+": date.getMonth() + 1,
      "d+": date.getDate(),
      "h+": date.getHours(),
      "m+": date.getMinutes(),
      "s+": date.getSeconds(),
      "q+": Math.floor((date.getMonth() + 3) / 3),
      "S+": date.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
      format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date_arr) {
      if (new RegExp("(" + k + ")").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date_arr[k] : ("00" + date_arr[k]).substr(("" + date_arr[k]).length));
      }
    }
    return format;
  }

  this.mapParams = {
    orgId: $rootScope.orgId || storageApi.get('orgId'),
    patrolTime: this.dateToStr(date, 'yyyy-MM-dd'),
  };

  this.pagination = {
    page: 1,
    size: 10,
    total: '',
  };

  $scope.query = () => {
    patrolService
      .queryWorker({
        mapParams: this.mapParams,
        page: this.pagination.page,
        size: this.pagination.size,
      })
      .then((res) => {
        if (res.success) {
          this.checkList = res.rows;
          if (this.checkList && this.checkList.length > 0) {
            this.hasCheckProcess = true;
            const max = _.max(this.checkList.map(i => i.patrolNums));
            this.checkList.forEach((item) => {
              const percentage = (item.patrolNums / max) * 100;
              item.percentage = `${percentage}%`;
              item.width = { width: item.percentage };
            });
          } else {
            this.hasCheckProcess = false;
          }
          this.pagination.total = res.total;
          if ((this.pagination.total % this.pagination.size) == 0 ) {
            this.pagination.pageNum = this.pagination.total / this.pagination.size;
          } else {
            this.pagination.pageNum = parseInt(this.pagination.total / this.pagination.size) + 1;
          }
        }
      });

    if (this.currentPage > this.pagination.pageNum || this.currentPage == 1) {
      this.disabledNextPage = true;
      this.disabledPrePage = true;
    } else {
      this.disabledNextPage = false;
      this.disabledPrePage = false;
    }
  };

  this.selectPage = (type) => {
    if (this.currentPage == 1 || this.currentPage == this.pagination.pageNum) {
      return false;
    }
    if (type === 'previous') {
      this.currentPage = this.currentPage - 1;
      this.currentPage = this.currentPage > 1 ? this.currentPage - 1 : 1;
    } else {
      this.currentPage = this.currentPage + 1;
      this.currentPage = this.currentPage < this.pagination.pageNum ? this.currentPage + 1 : this.currentPage;
    }
    this.pagination.page = this.currentPage;
    $scope.query();
  };

  // 上一天
  this.preTime = () => {
    this.middleDate = new Date(new Date(this.mapParams.patrolTime).getTime() - 24*60*60*1000);
    this.handleTime(this.middleDate);
  }

  this.nextTime = () => {
    this.middleDate = new Date(new Date(this.mapParams.patrolTime).getTime() + 24*60*60*1000);
    this.handleTime(this.middleDate);
  }

  this.handleTime = (middleDate) => {
    var currentDate = new Date(middleDate);
    this.month = currentDate.getMonth() + 1;
    if (this.month < 10) {
      this.month = '0' + this.month;
    }

    this.date = currentDate.getDate();
    if (this.date < 10) {
      this.date = '0' + this.date;
    }

    this.mapParams.patrolTime = currentDate.getFullYear() + '-' + this.month + '-' + this.date;
    $scope.query();
  }

  $scope.query();

  $("#datetimepicker").on("changeDate", function() {
    this.mapParams = {
      patrolTime: '',
      patrolUserId: '',
    };
    $scope.patrolTime = $("#datetimepicker").val();
    this.mapParams.patrolTime = $scope.patrolTime;
    $scope.query();
  });


  this.onSelectItem = (worker) => {
    this.ngModel = worker;
    // 把巡查时间传过去
    this.ngModel.patrolTime = this.mapParams.patrolTime;
  }
}

export default patrolBranchController;
